<?php
# Part of RASS unittest fixtures: item upload and mgmt.
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

class RassItemMgmtTest extends UnitTestCase {
    function TestRassClientlib() {
        $this->UnitTestCase("Rass Item Mgmt Tests");
    }

    # we don't use the regular setUp and tearDown functions here, cause this would cause too much traffic
    # instead we're relying on the fact that methods are executed in declaration order
    function testSetup() {
      try {
        $rass = new Rass(SUPERUSER, SUPERPWD, BASE);
        $newUser = $rass->createUser(USER, EMAIL, PWD);
        # create a subdir
        $rass = new Rass(USER, PWD, BASE);
        $entry = $rass->createDir(DIRNAME);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

    function testHeadNotExistingItem() {
      $status = 0;
      try {
        $rass = new Rass(USER, PWD, BASE);
        $head = $rass->getItemHeader(ITEMNAME);
        if ($head) {
          assert(false);
        }
        $status = $rass->getResponse()->getStatus();
        $status = $rass->getResponse()->getStatus();
      }
      catch(Zend_Gdata_App_Exception $e) {
      }
      $this->assertEqual($status, 404);
    }

    function testCreateItemUnderRoot() {
      try {
        $rass = new Rass(USER, PWD, BASE);
        $rass->createItem(ITEMNAME, LOCAL_FILE);
        $this->assertEqual($rass->getResponse()->getStatus(), 201);
        $this->assertEqual($rass->getResponse()->getHeader("Location"), BASE . "item/" . ITEMNAME . "/");
        }
        catch(Zend_Gdata_App_Exception $e) {
          assert(False);
        }
      }

    function testCreateSameItem() {
      $status = 0;
      try {
        $rass = new Rass(USER, PWD, BASE);
        $rass->createItem(ITEMNAME, LOCAL_FILE);
        }
        catch(Zend_Gdata_App_Exception $e) {
          $status = $e->getResponse()->getStatus();
        }
        $this->assertEqual($status, 409);
      }
        
    function testHeadItemUnderRoot() {
      try {
        $rass = new Rass(USER, PWD, BASE);
        $head = $rass->getItemHeader(ITEMNAME);
        $this->assertEqual($rass->getResponse()->getStatus(), 200);
        $this->assertEqual($rass->getResponse()->getHeader("Content-type"), MIMETYPE);
        $this->assertEqual($rass->getResponse()->getHeader("Content-length"), 13827);
        $this->assertEqual($rass->getResponse()->getHeader("Content-length"), $head->getHeader("Content-length"));
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

    function testDeleteItemUnderRoot() {
      try {
        $rass = new Rass(USER, PWD, BASE);
        $rass->deleteItem(ITEMNAME);
        $this->assertEqual($rass->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

    function testHeadDeletedItem() {
      $status = 0;
      try {
        $rass = new Rass(USER, PWD, BASE);
        $head = $rass->getItemHeader(ITEMNAME);
        if ($head) {
          assert(false);
        }
        $status = $rass->getResponse()->getStatus();
      }
      catch(Zend_Gdata_App_Exception $e) {
      }
      $this->assertEqual($status, 404);
    }
    
    function testTeardown() {
      try {
        $rass = new Rass(USER, PWD, BASE);
        $entry = $rass->deleteDir(DIRNAME);
        $rass = new Rass(SUPERUSER, SUPERPWD, BASE);
        $newUser = $rass->deleteUser(USER);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

}
